// Copyright (c) 2016 The Tencent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/tencent/network_utils.h"
#include "net/base/network_change_notifier.h"

#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_hints.h"
#include "net/base/address_list.h"
#include "net/base/load_flags.h"
#include "net/dns/host_resolver.h"
#include "net/dns/single_request_host_resolver.h"
#include "net/http/http_network_session.h"
#include "net/http/http_request_info.h"
#include "net/http/http_stream_factory.h"
#include "net/http/http_transaction_factory.h"
#include "net/url_request/http_user_agent_settings.h"
#include "net/url_request/url_request_context.h"

namespace net {

// static
bool NetworkUtils::IsNetworkWifi()
{
    return net::NetworkChangeNotifier::GetConnectionType()
        == NetworkChangeNotifier::CONNECTION_WIFI;
}

// static
bool NetworkUtils::IsNetworkCellular()
{
    return net::NetworkChangeNotifier::IsConnectionCellular(
        net::NetworkChangeNotifier::GetConnectionType());
}

// static
bool NetworkUtils::IsNetworkOffline()
{
    return net::NetworkChangeNotifier::IsOffline();
}

// static
void NetworkUtils::PreconnectUrl(URLRequestContext* context,
    const GURL& url,
    int count,
    bool is_direct,
    bool allow_credentials)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
    DCHECK(context);

    net::HttpTransactionFactory* factory = context->http_transaction_factory();
    net::HttpNetworkSession* session = factory->GetSession();

    std::string user_agent;
    if (context->http_user_agent_settings())
        user_agent = context->http_user_agent_settings()->GetUserAgent();
    net::HttpRequestInfo request_info;
    request_info.url = url;
    request_info.method = "GET";
    request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kUserAgent,
        user_agent);
    //  request_info.motivation = motivation;

    if (is_direct)
        request_info.load_flags |= LOAD_BYPASS_PROXY;

    //  net::NetworkDelegate* delegate = context->network_delegate();
    //  if (delegate->CanEnablePrivacyMode(url, first_party_for_cookies))
    //    request_info.privacy_mode = net::PRIVACY_MODE_ENABLED;

    // TODO(yoav): Fix this layering violation, since when credentials are not
    // allowed we should turn on a flag indicating that, rather then turn on
    // private mode, even if lower layers would treat both the same.
    if (!allow_credentials) {
        request_info.privacy_mode = net::PRIVACY_MODE_ENABLED;
        request_info.load_flags = net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_AUTH_DATA;
    }

    net::HttpStreamFactory* http_stream_factory = session->http_stream_factory();
    http_stream_factory->PreconnectStreams(count, request_info);
}

} // namespace net
